package com.fhzn.boss.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.boss.entity.User;
import com.fhzn.boss.web.converter.UserConverter;
import com.fhzn.boss.web.request.UserQueryRequest;
import com.fhzn.boss.web.vo.UserExportVO;

import java.util.List;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/22
 */
public interface UserMapper extends BaseMapper<User> {


    /**
     * 根据昵称查询用户
     *
     * @param nickname 用户昵称
     * @return 用户
     */
    default User findUserByNickName(String nickname) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getNickname, nickname);
        wrapper.eq(User::getStatus, 1);
        return selectOne(wrapper);
    }


    /**
     * 根据分页查询用户
     *
     * @param request 用户请求
     * @return 用户列表
     */
    default Page<User> query(UserQueryRequest request) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        if (request.getNickname() != null && !request.getNickname().isEmpty()) {
            wrapper.like(User::getNickname, request.getNickname());
        }
        if (request.getStatus() != null) {
            wrapper.eq(User::getStatus, request.getStatus());
        }
        wrapper.orderByDesc(User::getCreatedTime);
        return selectPage(Page.of(request.getPage(), request.getPageSize()), wrapper);
    }

    /**
     * 根据应用编码查询用户
     *
     * @param code 应用编码
     * @return 用户列表
     */
    default List<UserExportVO> findUsersByAppCode(String code) {
        List<User> users = selectList(Wrappers.lambdaQuery(User.class).eq(User::getAppCode, code));
        return UserConverter.fromUserList(users);
    }
}
